package com.cndw.kungfu.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.core.CacheDataBase;
import com.cndw.kungfu.core.CacheDataKey;
import com.cndw.kungfu.model.siege.domain.SiegeRanking;

public class ActivitySerialize extends CacheDataBase implements Serializable {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 4866400951076998300L;

	private static ActivitySerialize instance = null;

	/**
	 * 围城防守帮派(帮派Id,连胜次数)
	 */
	private int[] siegeUnionId = new int[] { 0, 0 };

	/**
	 * 围城排行版
	 */
	private List<SiegeRanking> siegeRanking = new LinkedList<SiegeRanking>();

	/**
	 * 端午节活动,提交礼券的记录
	 */
	private Map<Long, Integer> zongziLiQuanMap;

	private Map<Long, Integer> zongziFireMap;
	/**
	 * 奖池的银币
	 */
	private volatile int carnivalAsset;
	/**
	 * 上次大奖得主
	 */
	private long carivalAwarder;
	/**
	 * 上次得奖银两数
	 */
	private int carivalAwardAsset;
	
	private Map<Long, Integer> winneOrderMap;
	
	public int[] getSiegeUnionId() {
		return siegeUnionId;
	}

	public List<SiegeRanking> getSiegeRanking() {
		return siegeRanking;
	}

	public void setSiegeRanking(List<SiegeRanking> siegeRanking) {
		this.siegeRanking = siegeRanking;
	}

	public static ActivitySerialize getInstance() {
		if (instance == null) {
			synchronized (ActivitySerialize.class) {
				if (instance == null) {
					CacheDataBase cacheData = Context.getSysServer().initCacheData(CacheDataKey.keyActivitySerialize);
					if (cacheData != null && cacheData instanceof ActivitySerialize) {
						instance = (ActivitySerialize) cacheData;
					}
					if (instance == null) {
						instance = new ActivitySerialize();
					}
					if (instance.getSyncDBTime() == 0) {
						instance.commitSync();
					}
				}
			}
		}
		return instance;
	}

	@Override
	public String getKey() {
		return CacheDataKey.keyActivitySerialize;
	}

	public void zongZiLiQuanAdd(long playerId) {
		if (zongziLiQuanMap == null) {
			synchronized (ActivitySerialize.class) {
				if (zongziLiQuanMap == null) {
					zongziLiQuanMap = new ConcurrentHashMap<Long, Integer>();
				}
			}
		}
		int cNum = zongziLiQuanGet(playerId);
		zongziLiQuanMap.put(playerId, cNum + 1);
	}

	public void zongZiLiQuanSet(long playerId, int num) {
		if (num <= 0) {
			zongziLiQuanRemove(playerId);
		} else {
			zongziLiQuanMap.put(playerId, num);
		}

	}

	public int zongziLiQuanGet(long playerId) {
		if (zongziLiQuanMap == null || !zongziLiQuanMap.containsKey(playerId)) {
			return 0;
		}
		return zongziLiQuanMap.get(playerId);
	}

	public Map<Long, Integer> zongziFireMapGet() {
		if (zongziFireMap == null) {
			synchronized (ActivitySerialize.class) {
				if (zongziFireMap == null) {
					zongziFireMap = new ConcurrentHashMap<Long, Integer>();
				}
			}
		}
		return zongziFireMap;
	}

	public void zongziLiQuanRemove(long playerId) {
		if (zongziLiQuanMap != null) {
			zongziLiQuanMap.remove(playerId);
		}
	}

	public int getCarnivalAsset() {
		if (carnivalAsset <= 0) {
			carnivalAsset = ConstantsModel.carnivalAssetInit;
		}
		return carnivalAsset;
	}

	public void setCarnivalAsset(int carnivalAsset) {
		this.carnivalAsset = carnivalAsset;
	}

	public void alterCarnivalAsset(int alter) {
		if (alter >0 && Integer.MAX_VALUE - alter < carnivalAsset) {
			return ;
		}
		carnivalAsset += alter;
	}

	public long getCarivalAwarder() {
		return carivalAwarder;
	}

	public void setCarivalAwarder(long carivalAwarder) {
		this.carivalAwarder = carivalAwarder;
	}

	public int getCarivalAwardAsset() {
		return carivalAwardAsset;
	}

	public void setCarivalAwardAsset(int carivalAwardAsset) {
		this.carivalAwardAsset = carivalAwardAsset;
	}

	public Map<Long, Integer> getWinneOrderMap() {
		if (winneOrderMap == null) {
			synchronized (this) {
				if (winneOrderMap == null) {
					winneOrderMap = new HashMap<Long, Integer>();
				}
			}
		}
		return winneOrderMap;
	}

	
}
